package com.kingtroldata.monitorview_demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kingtroldata.monitorview.MonitorView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MonitorView mv;
    private Spinner spinner;
    private TextView et_x;
    private TextView et_y;
    private Button x_add;
    private Button x_reduce;
    private Button y_add;
    private Button y_reduce;
    private Button setting;
    private Button refresh;
    private Button reset;

    private int type;
    private String factoryId = "1151041276";
    private String manageId = "17";
    //图片的偏移量
    private int image_x;
    private int image_y;
    //文字的偏移量
    private int text_x;
    private int text_y;
    //数字的偏移量
    private int data_x;
    private int data_y;
    private String preUrl;
    private String config;
    private String bgUrl;
    private List<DataBean.ListBean> dataList;
    private List<ConfigBean.PicBean> picBeanList;
    private List<ConfigBean.ContentBean> contentBeanList;
    private float dataSize;
    private String textColor;
    private float textSize;
    private String dataColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initSpinner();
        getConfiguration();//获取配置信息
    }


    //获取背景图片
    private void getBG(String url) {
        OkGo.get(url)
                .tag(this)
                .execute(new BitmapCallback() {
                    @Override
                    public void onSuccess(Bitmap bitmap, Call call, Response response) {
                        mv.addBackground(bitmap);//设置背景图片
                        getData();
                        for (ConfigBean.ContentBean b : contentBeanList) {//文字
                            String text = b.monitorLocationName;
                            int x = (int) b.monitorLocationX;
                            int y = (int) b.monitorLocationY;
                            mv.addTextView(text, dataSize, Color.parseColor(textColor), x, y);
                        }
                    }
                });
    }

    /**
     * 获取具体数据
     */
    public void getData() {
        OkGo.get(URLS.URL_DATA)
                .tag(this)
                .params("dataType", 1)
                .params("factoryId", factoryId)
                .params("manageId", manageId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LoggerUtils.e("具体数据获取成功" + s);

                        resData(s);//解析具体数据
                        setData();//设置设局
                    }
                });
    }

    /**
     * 解析具体数据
     *
     * @param result
     */
    private void resData(String result) {
        Gson gson = new Gson();
        DataBean dataBean = gson.fromJson(result, DataBean.class);
        dataList = dataBean.list;
    }

    /**
     * 获取配置信息
     */
    private void getConfiguration() {
        mv.cleanMonitorView();

        LoggerUtils.e("image_x:" + image_x + ",image_y:" + image_y + "\r\n"
                + "text_x:" + text_x + ",text_y;" + text_y + "\r\n"
                + "data_x:" + data_x + ",data_y:" + data_y);
        mv.setImageTranslate(image_x, image_y);//设置图片偏移量
        mv.setTextTranslate(text_x, text_y);//设置问题偏移量
        mv.setDataTranslate(data_x, data_y);//设置数据偏移量

        OkGo.get(URLS.URL_CONFIG)
                .tag(this)
                .params("factoryId", factoryId)
                .params("manageId", manageId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LoggerUtils.e("配置数据获取成功");
                        config = s.replace("\\r\\n", "");
                        resConfigData(config);//解析配置数据
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        LoggerUtils.e("配置数据获取失败");
                    }
                });
    }

    /**
     * 解析配置数据
     *
     * @param data
     */
    public void resConfigData(String data) {
        Gson gson = new Gson();
        ConfigBean bean = gson.fromJson(data, ConfigBean.class);
        ConfigBean.ConfigurationBean c = bean.configuration;
        dataSize = c.dataSize;
        dataColor = c.dataColor;
        textColor = c.textColor;
        textSize = c.textSize;
        picBeanList = bean.pic;
        contentBeanList = bean.content;
        preUrl = c.preUrl;
        float designX = c.designX;
        float designY = c.designY;
        bgUrl = c.bgUrl;
        mv.setDesign(designX, designY);//设置模板尺寸
        getBG(preUrl + bgUrl);//获取背景图片

    }

    private void setData() {
        for (ConfigBean.PicBean b : picBeanList) {//图片和数据的坐标
            int x = (int) b.monitorLocationX;//x轴的坐标
            int y = (int) b.monitorLocationY;//y轴的坐标
            int width = (int) b.monitorLocationImgWidth;
            int height = (int) b.monitorLocationImgHeight;
            int fieldType = b.monitorFieldType;
            String monitorFieldCode = b.monitorFieldCode;

            for (DataBean.ListBean bean : dataList) {
                String imgPath = bean.imgPath;
                String value = bean.value;
                String name = bean.name;

                if (monitorFieldCode.equals(name)) {
                    if (fieldType == 0) {
                        mv.addDataView(value + "", dataSize, Color.parseColor(dataColor), x, y);
                    } else {

                        if (value.equals("2")) {
                            imgPath = imgPath.replaceAll("-r", "-f");
                            imgPath = imgPath.replaceAll("-s", "-f");
                        } else if (value.equals("1")) {
                            imgPath = imgPath.replaceAll("-s", "-r");
                            imgPath = imgPath.replaceAll("-f", "-r");
                        } else if (value.equals("0")) {
                            imgPath = imgPath.replaceAll("-r", "-s");
                            imgPath = imgPath.replaceAll("-f", "-s");
                        }

                        setPic(preUrl + imgPath, width, height, x, y);
                    }
                }
            }
        }
    }

    private void initView() {
        mv = (MonitorView) findViewById(R.id.mv);
        spinner = (Spinner) findViewById(R.id.spinner);
        et_x = (TextView) findViewById(R.id.et_x);
        et_y = (TextView) findViewById(R.id.et_y);
        x_add = (Button) findViewById(R.id.bt_x_add);
        x_reduce = (Button) findViewById(R.id.bt_x_reduce);
        y_add = (Button) findViewById(R.id.bt_y_add);
        y_reduce = (Button) findViewById(R.id.bt_y_reduce);
        setting = (Button) findViewById(R.id.bt_setting);
        refresh = (Button) findViewById(R.id.refresh);
        reset = (Button) findViewById(R.id.bt_reset);

        x_add.setOnClickListener(this);
        x_reduce.setOnClickListener(this);
        y_add.setOnClickListener(this);
        y_reduce.setOnClickListener(this);
        setting.setOnClickListener(this);
        refresh.setOnClickListener(this);
        reset.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        factoryId = data.getStringExtra("factoryId");
        manageId = data.getStringExtra("manageId");
        if (factoryId != null && manageId != null)
            Toast.makeText(this, "factoryId:" + factoryId + "\r\n" + "manageId:" + manageId, Toast.LENGTH_SHORT).show();
    }

    private void reset() {
        //图片的偏移量
        image_x = 0;
        image_y = 0;
        //文字的偏移量
        text_x = 0;
        text_y = 0;
        //数字的偏移量
        data_x = 0;
        data_y = 0;
        et_x.setText(0 + "");
        et_y.setText(0 + "");
        getConfiguration();
    }

    //设置图片
    private void setPic(String picUrl, final int w, final int h, final int x, final int y) {
        OkGo.get(picUrl)
                .tag(this)
                .execute(new BitmapCallback() {
                             @Override
                             public void onSuccess(final Bitmap bitmap, Call call, Response response) {
                                 mv.addImageView(bitmap, w, h, x, y);//添加图片
                             }

                             @Override
                             public void onError(Call call, Response response, Exception e) {
                                 super.onError(call, response, e);
                                 LoggerUtils.e("图片加载错误");
                             }
                         }
                );
    }

    //X轴偏移
    private void translateX(boolean isAdd) {
        int x = 0;
        if (isAdd) {
            switch (type) {
                case 0://图片偏移量
                    x = ++image_x;
                    break;
                case 1://文字偏移量
                    x = ++text_x;
                    break;
                case 2://数字偏移量
                    x = ++data_x;
                    break;
            }
        } else {
            switch (type) {
                case 0://图片偏移量
                    x = --image_x;
                    break;
                case 1://文字偏移量
                    x = --text_x;
                    break;
                case 2://数字偏移量
                    x = --data_x;
                    break;
            }
        }
        et_x.setText(x + "");
    }

    //Y轴偏移
    private void translateY(boolean isAdd) {
        int y = 0;
        if (isAdd) {
            switch (type) {
                case 0://图片偏移量
                    y = ++image_y;
                    break;
                case 1://文字偏移量
                    y = ++text_y;
                    break;
                case 2://数字偏移量
                    y = ++data_y;
                    break;
            }
        } else {
            switch (type) {
                case 0://图片偏移量
                    y = --image_y;
                    break;
                case 1://文字偏移量
                    y = --text_y;
                    break;
                case 2://数字偏移量
                    y = --data_y;
                    break;
            }
        }
        et_y.setText(y + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_setting://设置
                startActivityForResult(new Intent(this, SettingActivity.class), 0);
                break;
            case R.id.refresh://刷新数据
                getConfiguration();
                break;
            case R.id.bt_reset://重置
                reset();
                break;
            case R.id.bt_x_add://x加
                translateX(true);
                break;
            case R.id.bt_x_reduce://x减
                translateX(false);
                break;
            case R.id.bt_y_add://y加
                translateY(true);
                break;
            case R.id.bt_y_reduce://y减
                translateY(false);
                break;
        }
    }

    private void initSpinner() {
        List<String> list = new ArrayList<>();
        list.add("图片偏移量");
        list.add("文字偏移量");
        list.add("数据偏移量");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                type = position;
                switch (type) {
                    case 0://图片偏移量
                        et_x.setText(image_x + "");
                        et_y.setText(image_y + "");
                        break;
                    case 1://文字偏移量
                        et_x.setText(text_x + "");
                        et_y.setText(text_y + "");
                        break;
                    case 2://数字偏移量
                        et_x.setText(data_x + "");
                        et_y.setText(data_y + "");
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
